package io.github.consoles.dsa;

import java.util.ConcurrentModificationException;
import java.util.Iterator;

/**
 * Created by yiihua-013 on 17/1/22.
 * <p>
 * 下压栈:链表实现
 * <p>
 * 实现Iterable接口可以用for-each循环
 */
public class LinkedStack<Item> implements Iterable<Item> {

    private Node top; // 栈顶
    private int size;

    /**
     * 快速出错的迭代器
     *
     * 记录push和pop的次数,每次遍历的时候检查下该值,如果不等,则抛出ConcurrentModificationException
     *
     * 参见:http://www.importnew.com/20299.html
     * JDK中的快速失败机制:
     */
    private int modifyCount;

    /**
     * 得到stack的副本
     *
     * @param stack
     * @see ArrayQueue#ArrayQueue(ArrayQueue)
     */
    public LinkedStack(LinkedStack<Item> stack) {
        top = null;
        size = 0;
        LinkedStack<Item> tmp = new LinkedStack<Item>();
        for (Item item : stack) {
            tmp.push(item);
        }
        for (Item item : tmp) {
            this.push(item);
        }
    }

    public LinkedStack() {

    }

    private class Node {
        Item item;
        Node next;
    }

    public boolean isEmpty() {
        return size == 0; // or top == null
    }

    public int size() {
        return size;
    }

    /**
     * 压栈
     * 链表头部插入
     *
     * @param item
     */
    public void push(Item item) {
        Node oldFirst = top;
        top = new Node();
        top.item = item;
        top.next = oldFirst;
        size++;
        modifyCount++;
    }

    /**
     * 弹出栈
     * 链表头部删除
     *
     * @return
     */
    public Item pop() {
        Item item = top.item;
        top = top.next; // item由于没有指针指向,gc
        size--;
        modifyCount++;
        return item;
    }

    /**
     * 取栈顶
     *
     * @return
     */
    public Item peek() {
        return top.item;
    }

    @Override
    public Iterator<Item> iterator() {
        return new LinkedIterator();
    }

    private class LinkedIterator implements Iterator<Item> {

        private Node current = top;
        private int mCount = modifyCount;

        /**
         * 每次调用next,current指针将会变化,从而导致hasNext变化
         *
         * @return
         */
        @Override
        public boolean hasNext() {
            if (modifyCount != mCount) throw new ConcurrentModificationException();
            return current != null;
        }

        @Override
        public Item next() {
            if (modifyCount != mCount) throw new ConcurrentModificationException();
            Item item = current.item;
            current = current.next;
            return item;
        }
    }

    /**
     * 连接2个栈
     *
     * @param stack
     * @return
     */
    public LinkedStack<Item> catenation(LinkedStack<Item> stack) {
        if (isEmpty()) {
            top = stack.top;
            size = stack.size;
        } else {
//            LinkedStack<Item> tmp = new LinkedStack<Item>();
//            while (!s2.isEmpty()) {
//                tmp.push(s2.pop());
//            }
//            while (!tmp.isEmpty()) {
//                s1.push(tmp.pop());
//            }

            // 不用临时栈,直接改变引用
            Node first = stack.top;
            while (first.next != null) first = first.next;
            first.next = top;
            top = stack.top;
            size += stack.size;
        }
        stack = null; // call by value
        return this;
    }
}
